package com.shizhanzhe.zxbapp.my;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.ui.LoginActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zz9527 on 2019/1/16.
 */
public class MyWXActivity extends Activity {
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.ts)
    TextView ts;
    @BindView(R.id.et_wx)
    EditText etWx;
    @BindView(R.id.ll_wbd)
    LinearLayout llWbd;
    @BindView(R.id.tv_wx)
    TextView tvWx;
    @BindView(R.id.rl_ybd)
    RelativeLayout rlYbd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx);
        ButterKnife.bind(this);
        ts.setSelected(true);
        if ("".equals(UserBean.wx)){
            llWbd.setVisibility(View.VISIBLE);
        }else {
            rlYbd.setVisibility(View.VISIBLE);
            tvWx.setText(UserBean.wx);
        }
    }
    private void getData(final String wx){
        OkHttpDownloadJsonUtil.downloadJson(this, Path.WX(UserBean.uid,wx), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    Toast.makeText(MyWXActivity.this, object.optString("info"), Toast.LENGTH_SHORT).show();
                    if (object.optInt("status")==1){
                        UserBean.wx=wx;
                        SharedPreferencesUtils.setParam(MyWXActivity.this, "wx", UserBean.wx);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    @OnClick({R.id.backarea, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backarea:
                finish();
                break;
            case R.id.tv_commit:
                if ("".equals(etWx.getText().toString())){
                    Toast.makeText(this, "微信不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    getData(etWx.getText().toString());
                }
                break;
            default:
                break;
        }
    }
}
